<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
class ContentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $info = DB::table('post')
                ->leftJoin('allusers','post.uid','=','allusers.id')
                ->get();
        
       return view('admin.content.index',['list'=>$info]);
       
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.content.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       
            
            //获取指定的部分数据
            $data['title'] = $request->input('title');
            $data['head_title'] = $request->input('head_title');
 
            if(!$request->hasFile('pic')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('pic');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['img'] = $img->store(date('Ymd'));
            
            $data['created_at'] = date("Y-m-d H:i:s");
            
             $id = \DB::table("post")->insertGetId($data);
        
           if($id>0){
                 return redirect('content');
             }else{
                return back()->with("err","添加失败!");
          } 
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
